package com.example.wechat.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class WeChatDatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "wechat.db";
    private static final int DATABASE_VERSION = 1;

    // 用户表
    public static final String TABLE_USERS = "users";
    public static final String COLUMN_USER_ID = "user_id";
    public static final String COLUMN_USERNAME = "username";
    public static final String COLUMN_PASSWORD = "password";
    public static final String COLUMN_NICKNAME = "nickname";
    public static final String COLUMN_AVATAR = "avatar";
    public static final String COLUMN_SIGNATURE = "signature";

    // 聊天记录表
    public static final String TABLE_MESSAGES = "messages";
    public static final String COLUMN_MESSAGE_ID = "message_id";
    public static final String COLUMN_SENDER_ID = "sender_id";
    public static final String COLUMN_RECEIVER_ID = "receiver_id";
    public static final String COLUMN_MESSAGE_CONTENT = "content";
    public static final String COLUMN_TYPE = "type"; // 0:文本 1:图片 2:语音 3:视频
    public static final String COLUMN_TIMESTAMP = "timestamp";
    public static final String COLUMN_STATUS = "status"; // 0:未读 1:已读

    // 好友表
    public static final String TABLE_CONTACTS = "contacts";
    public static final String COLUMN_CONTACT_ID = "contact_id";
    public static final String COLUMN_USER_ID_1 = "user_id_1";
    public static final String COLUMN_USER_ID_2 = "user_id_2";
    public static final String COLUMN_REMARK = "remark";
    public static final String COLUMN_GROUP = "group_name";

    // 朋友圈表
    public static final String TABLE_MOMENTS = "moments";
    public static final String COLUMN_MOMENT_ID = "moment_id";
    public static final String COLUMN_PUBLISHER_ID = "publisher_id";
    public static final String COLUMN_MOMENT_CONTENT = "content";
    public static final String COLUMN_IMAGES = "images"; // 图片URL，多个用逗号分隔
    public static final String COLUMN_LIKES = "likes"; // 点赞用户ID，多个用逗号分隔
    public static final String COLUMN_COMMENTS = "comments"; // 评论内容，JSON格式存储
    public static final String COLUMN_PUBLISH_TIME = "publish_time";

    public WeChatDatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // 创建用户表
        String createUsersTable = "CREATE TABLE " + TABLE_USERS + " (" +
                COLUMN_USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_USERNAME + " TEXT UNIQUE NOT NULL, " +
                COLUMN_PASSWORD + " TEXT NOT NULL, " +
                COLUMN_NICKNAME + " TEXT, " +
                COLUMN_AVATAR + " TEXT, " +
                COLUMN_SIGNATURE + " TEXT" +
                ");";
        db.execSQL(createUsersTable);

        // 创建聊天记录表
        String createMessagesTable = "CREATE TABLE " + TABLE_MESSAGES + " (" +
                COLUMN_MESSAGE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_SENDER_ID + " INTEGER NOT NULL, " +
                COLUMN_RECEIVER_ID + " INTEGER NOT NULL, " +
                COLUMN_MESSAGE_CONTENT + " TEXT NOT NULL, " +
                COLUMN_TYPE + " INTEGER DEFAULT 0, " +
                COLUMN_TIMESTAMP + " INTEGER NOT NULL, " +
                COLUMN_STATUS + " INTEGER DEFAULT 0, " +
                "FOREIGN KEY(" + COLUMN_SENDER_ID + ") REFERENCES " + TABLE_USERS + "(" + COLUMN_USER_ID + "), " +
                "FOREIGN KEY(" + COLUMN_RECEIVER_ID + ") REFERENCES " + TABLE_USERS + "(" + COLUMN_USER_ID + ")" +
                ");";
        db.execSQL(createMessagesTable);

        // 创建好友表
        String createContactsTable = "CREATE TABLE " + TABLE_CONTACTS + " (" +
                COLUMN_CONTACT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_USER_ID_1 + " INTEGER NOT NULL, " +
                COLUMN_USER_ID_2 + " INTEGER NOT NULL, " +
                COLUMN_REMARK + " TEXT, " +
                COLUMN_GROUP + " TEXT, " +
                "FOREIGN KEY(" + COLUMN_USER_ID_1 + ") REFERENCES " + TABLE_USERS + "(" + COLUMN_USER_ID + "), " +
                "FOREIGN KEY(" + COLUMN_USER_ID_2 + ") REFERENCES " + TABLE_USERS + "(" + COLUMN_USER_ID + "), " +
                "UNIQUE(" + COLUMN_USER_ID_1 + ", " + COLUMN_USER_ID_2 + ")" +
                ");";
        db.execSQL(createContactsTable);

        // 创建朋友圈表
        String createMomentsTable = "CREATE TABLE " + TABLE_MOMENTS + " (" +
                COLUMN_MOMENT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_PUBLISHER_ID + " INTEGER NOT NULL, " +
                COLUMN_MOMENT_CONTENT + " TEXT, " +
                COLUMN_IMAGES + " TEXT, " +
                COLUMN_LIKES + " TEXT, " +
                COLUMN_COMMENTS + " TEXT, " +
                COLUMN_PUBLISH_TIME + " INTEGER NOT NULL, " +
                "FOREIGN KEY(" + COLUMN_PUBLISHER_ID + ") REFERENCES " + TABLE_USERS + "(" + COLUMN_USER_ID + ")" +
                ");";
        db.execSQL(createMomentsTable);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // 删除旧表
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_MOMENTS);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_MESSAGES);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
        
        // 创建新表
        onCreate(db);
    }

    @Override
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // 处理版本降级，与升级逻辑相同
        onUpgrade(db, oldVersion, newVersion);
    }
} 